#!/bin/bash

#目前版本必须先安装或更新yasm
#

download_url='http://ffmpeg.org/releases/ffmpeg-4.4.1.tar.gz'
download_path='/dl/ffmpeg'
tarFile='ffmpeg-4.4.1.tar.gz'
sourceFile='ffmpeg-4.4.1'
install_path='/basic/ffmpeg'

install_yasm() {
  if [ -e '/usr/local/include/libyasm' ]; then
    echo 'yasm已安装'
    return
  elif [ -e './yasm_install.sh' ]; then
    chmod 777 yasm_install.sh
    ./yasm_install.sh
  else
    echo 'yasm安装脚本呢？'
    return
  fi
}

createDir() {
  mkdir -p $download_path
  mkdir -p $install_path
}

download() {
  cd $download_path || exit
  wget $download_url
  if [ -e $tarFile ]; then
    tar -zxvf $tarFile
    echo '下载并解压完毕'
  else
    echo '下载压缩包失败'
    exit
  fi
}

install() {
  cd $sourceFile || exit
  ./configure --enable-shared --prefix=$install_path
  make
  make install PREFIX=$install_path
  echo '编译、安装完毕'
}

libConfig() {
  #  注册动态库文件目录
  cat <<'EOF' >>'/etc/ld.so.conf'

/basic/ffmpeg/lib
EOF

  #  刷新动态库缓存
  sudo ldconfig
  echo '动态链接库配置完毕'
}

register() {
  local profile
  profile='/etc/profile'
  cat <<'EOF' >>$profile

#set for ffmpeg
export PATH=$PATH:/basic/ffmpeg/bin
EOF

  source /etc/profile
  echo '环境变量注册完毕'
}

install_yasm
createDir
download
install
libConfig
register
ffmpeg -version
